package cn.kengtion.shopcart.View;

import android.content.Context;
import android.graphics.Paint;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.List;

import cn.kengtion.shopcart.Bean.GoodBean;
import cn.kengtion.shopcart.Listener.AbsCartListener;
import cn.kengtion.shopcart.R;

/**
 * Created by aaa on 2017/11/1.
 */

public class CartListAdapter extends RecyclerView.Adapter {
    private List<GoodBean> goodBeans;
    private LayoutInflater inflater;
    private boolean isGuide = false;
    private AbsCartListener cartListener;
    private Context ctx;

    public CartListAdapter(Context ctx, AbsCartListener listener) {
        this.ctx = ctx;
        inflater = LayoutInflater.from(ctx);
        this.cartListener = listener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(inflater.inflate(R.layout.yt_goods_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder && goodBeans != null) {
            final GoodBean goods = goodBeans.get(position);
            if (goods == null) {
                return;
            } else {
                ((ItemViewHolder) holder).goodsName.setText(goods.getGoodsName());
                ((ItemViewHolder) holder).MarketPrice.setText("￥" + String.valueOf(goods.getMarketPrice()));
                ((ItemViewHolder) holder).MarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                ((ItemViewHolder) holder).goodsPrice.setText("￥" + String.valueOf(goods.getRealPirce()));
                Picasso.with(ctx).load(goods.getGoodsImage()).into(((ItemViewHolder) holder).goodsImage);
                if (isGuide) {
                    ((ItemViewHolder) holder).minus.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (goods.getNum() != 1) {
                                goods.setNum(goods.getNum() - 1);
                                cartListener.changeGoodsNum(goods);
                                notifyDataSetChanged();
                            } else {
                                goodBeans.remove(goods);
                                cartListener.deleteGoods(goods);
                                notifyDataSetChanged();
                            }
                        }
                    });
                    ((ItemViewHolder) holder).add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            goods.setNum(goods.getNum() + 1);
                            cartListener.changeGoodsNum(goods);
                            notifyDataSetChanged();
                        }
                    });
                } else {
                    ((ItemViewHolder) holder).minus.setVisibility(View.GONE);
                    ((ItemViewHolder) holder).add.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        if (goodBeans != null)
            return goodBeans.size();
        return 0;
    }

    public void setGoodBeans(List<GoodBean> goodBeans) {
        this.goodBeans = goodBeans;
        notifyDataSetChanged();
    }

    public void setGuide(boolean guide) {
        isGuide = guide;
    }

    public double getTotalPrice() {
        double total = 0;
        for (GoodBean goodBean : goodBeans) {
            total += goodBean.getRealPirce();
        }
        DecimalFormat df   = new DecimalFormat("######0.00");
        df.format(total);
        return total;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        public TextView goodsPrice;
        public TextView MarketPrice;
        public TextView goodsName;
        public ImageView goodsImage;
        public ImageView add;
        public ImageView minus;

        public ItemViewHolder(View v) {
            super(v);
            goodsPrice = (TextView) v.findViewById(R.id.goods_price);
            MarketPrice = (TextView) v.findViewById(R.id.market_price);
            goodsName = (TextView) v.findViewById(R.id.goods_name);
            goodsImage = (ImageView) v.findViewById(R.id.goods_image);
            add = (ImageView) v.findViewById(R.id.add_goods);
            minus = (ImageView) v.findViewById(R.id.minus_goods);
        }
    }
}
